//
//  MainDefaulSearchVC_V4_Sub_Tbv.m
//  ZITHER
//
//  Created by weixin on 2018/11/28.
//  Copyright © 2018 weixn. All rights reserved.
//

#import "MainDefaulSearchVC_V4_Sub_Tbv.h"
#import "MainDefaulSearchVC_V4_Sub_TbvCell.h"
#import "GoodsModel.h"

@implementation MainDefaulSearchVC_V4_Sub_Tbv

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.delegate = self;
        self.dataSource = self;
        [self loadData];
    }
    return self;
}

/** tbv代理 */
#pragma MARK - 数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.State == STRefreshStateGoods) {
        return self.modelGoods.goodVOs.count;
    }
    return self.model.options.count+1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *sectionCell = @"MainDefaulSearchVC_V4_Sub_TbvCell";
    MainDefaulSearchVC_V4_Sub_TbvCell *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    if (cell == nil) {
        cell = [[MainDefaulSearchVC_V4_Sub_TbvCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    
    NSString *str = self.dic[@"num"];
    if (indexPath.row == 0) {
        if (str.length == 0) {
            cell.lab.textColor = [UIColor whiteColor];
            cell.lab.backgroundColor = [UIColor clearColor];
            cell.viewBorder.backgroundColor =  [UIColor colorWithHex:@"333333"];
        }else{
            cell.lab.textColor = [UIColor colorWithHex:@"333333"];
            cell.lab.backgroundColor = [UIColor clearColor];
            cell.viewBorder.backgroundColor = [UIColor whiteColor];
        }
        cell.lab.text = @"不限";
        
    }else{
        
        if ([str intValue] != indexPath.row-1 || str.length == 0) {
            
            cell.lab.textColor = [UIColor colorWithHex:@"333333"];
            cell.lab.backgroundColor = [UIColor clearColor];
            cell.viewBorder.backgroundColor = [UIColor whiteColor];
        }else{
            cell.lab.textColor = [UIColor whiteColor];
            cell.lab.backgroundColor = [UIColor clearColor];
            cell.viewBorder.backgroundColor =  [UIColor colorWithHex:@"333333"];
        }
        
        if (self.State == STRefreshStateGoods) {
            cell.lab.text = self.modelGoods.goodVOs[indexPath.row-1].goodInfo.goodName;
        }else{
            cell.lab.text = self.model.options[indexPath.row-1].name;
        }
        
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.State == STRefreshStateGoods) {
        
        if (indexPath.row == 0) {
            if (self.blcokGoods) {
                NSDictionary *dic = @{@"title":self.dic[@"title"],@"num":@""};
//                self.modelGoods.goodVOs[indexPath.row-1].goodInfo
                goodVOsModel *model3 = [[goodVOsModel alloc]init];
                goodVOsSubModel *mode4 = [[goodVOsSubModel alloc]init];
                mode4.goodName = @"型号";
                model3.goodInfo = mode4;
                self.blcokGoods(dic,model3);
            }
        }else{
            if (self.blcokGoods) {
                NSDictionary *dic = @{@"title":self.dic[@"title"],@"num":[NSString stringWithFormat:@"%ld",indexPath.row-1]};
                self.blcokGoods(dic,self.modelGoods.goodVOs[indexPath.row-1]);
            }
        }

    }else{
        if (indexPath.row == 0) {
            if (self.blcok) {
                NSDictionary *dic = @{@"title":self.dic[@"title"],@"num":@""};
                self.blcok(dic);
            }
        }else{
            if (self.blcok) {
                NSDictionary *dic = @{@"title":self.dic[@"title"],@"num":[NSString stringWithFormat:@"%ld",indexPath.row-1]};
                self.blcok(dic);
            }
        }
    }

    
   
    
    
    [self.getNav popViewControllerAnimated:YES];
}


-(void)loadData{
    
    /** 处理好数据传值 */
    NSDictionary *dic = [self setDicloadDate];
//    NSString *sort = @"secondHandPrice,asc";
//    if (self.search.btn3.tag == 300)   sort = @"secondHandPrice,desc";
        NSString *sort = @"secondHandPrice,desc";
//    NSString *sort = @"second_hand_price";
    //sort
    [[serviceDefualt loadGoodsList:dic page:self.page sort:sort reponseObjctClass:[GoodsModel class]]subscribeNext:^(ResponseData* x) {
        GoodsModel *modelRes = x.responseData;
        if (x.success) {
            
            if (modelRes.goodVOs.count == 0) {
                
                if (self.page == 0) {
                    [Tools showTips:@"没有搜到数据"];
                    [self.mj_header endRefreshing];
                    [self.mj_footer endRefreshing];
                    GoodsModel * goodVOsM = [[GoodsModel alloc]init];
                    self.modelGoods = goodVOsM;
                    
                }else{
                    [self.mj_footer endRefreshingWithNoMoreData];
                }
                
            }else{
                if (self.page == 0) {
                    
                    self.modelGoods = modelRes;
                    
                }else{
                    for (goodVOsModel *goodVOsModel1 in modelRes.goodVOs) {
                        [self.modelGoods.goodVOs addObject:goodVOsModel1];
                    }
                }
                [self reloadData];
                
            }
            
        }else{
             [Tools showTips:x.errorInfo];
        }
        [self endRefreshing];
    }];
}

-(void)Starefreshing{
    [self loadData];
}

-(NSMutableDictionary *)setDicloadDate{
    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    
    //    MainDiscernSelectWatchSeriesModelSub *modelSub = self.arrMWatchSeries[[self.WatchSeriesIndex intValue]];
    [dic setValue:self.modelSub.id forKey:@"series"];
    
    
    return dic;
    
}

@end
